package com.byzk.p2p.front.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.byzk.p2p.admin.base.controller.BaseController;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.web.dto.Article;
import com.byzk.p2p.admin.web.dto.ArticleType;
import com.byzk.p2p.admin.web.service.ArticleService;
import com.byzk.p2p.admin.web.vo.ArticleVo;

/**
 * 新闻相关
 * @author suker.zhou
 *
 */
@Controller
@Scope("prototype")
public class FrontNewsController extends BaseController implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@Autowired
	private ArticleService articleService;
	
	/**
	 * 进入新标公告页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/notice")
	public ModelAndView notice(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "notice");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/notice");
	}

	/**
	 * 进入网站公告页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/web")
	public ModelAndView website(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "web");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/web");
	}
	
	/**
	 * 进入公司动态页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/company")
	public ModelAndView company(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "companyTrends");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/company");
	}
	
	/**
	 * 进入有问必答页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/reply")
	public ModelAndView fund(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "reply");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/fund");
	}
	
	/**
	 * 进入投资交流页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/interflow")
	public ModelAndView news(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "interflow");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/news");
	}
	
	/**
	 * 进入媒体报道页面
	 * @param model
	 * @param index
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/news/media")
	public ModelAndView media(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "mediaReports");
		queryMap.put("typeId", 1);
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return new ModelAndView("/front/new/media");
	}
	
	/**
	 * 详情页面
	 * @param model
	 * @param id 新闻ID号
	 * @return
	 */
	@RequestMapping(value="/news/detail" , method=RequestMethod.GET)
	public ModelAndView news(Model model,@RequestParam Integer id,@RequestParam String type){
		ArticleVo vo = articleService.getArticleById(id);
		if (vo != null) {
			//上一篇
			HashMap<String, Object> ltMap = new HashMap<String, Object>();
			ltMap.put("id", id);
			ltMap.put("type", type);
			ltMap.put("status", 1);//已发布
			ltMap.put("matchLt", "matchLt");
			//下一篇
			HashMap<String, Object> gtMap = new HashMap<String, Object>();
			gtMap.put("id", id);
			gtMap.put("type", type);
			gtMap.put("status", 1);//已发布
			gtMap.put("matchGt", "matchGt");
			Article ltArticle = articleService.getAdjacentArticle(ltMap);
			Article gtArticle = articleService.getAdjacentArticle(gtMap);
			model.addAttribute("ltArticle", ltArticle);
			model.addAttribute("gtArticle", gtArticle);
			model.addAttribute("type", type);
			List<ArticleType>  listType = articleService.articleTypeList();
			model.addAttribute("listType", listType);
		}
		model.addAttribute("article", vo);
		return new ModelAndView("/front/new/detail");
	}
	

}
